/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package poolphysics;

/**
 *
 * @author Gerrit
 */
public class Ball {
    private double d,m; /**
                         * d is the diameter in meters
                         * m is the mass in kg
     */

    private Vector p,v,w; /**
                           * p is the position
                           * v is the velocity
                           * w is the spin
                           */

    public Ball(double diameter, double mass) {
        setDiameter(diameter);
        setMass(mass);
        p = new Vector(0,0,0);
    }
    public Ball(double diameter, double mass, Vector initialpos) {
        setDiameter(diameter);
        setMass(mass);
        p = initialpos;
    }

    public double getDiameter() {
        return d;
    }
    
    public void setDiameter(double diameter) {
        d = diameter;
    }

    public double getMass() {
        return m;
    }

    public void setPosition(Vector position) {
        p = position;
    }
    
    public void setMass(double mass) {
        m = mass;
    }

    public void hit(double a, double b, double verangle, double horangle, double v0, double cuestickmass) {

        double c = Math.abs
                (
                    Math.sqrt
                    (
                        Math.pow(d,2.0) -
                        Math.pow(a,2.0) -
                        Math.pow(b, 2.0)
                    )
                );

        double R = d/2;
        double F = (2*m*v0)/
                (
                    1+(m/cuestickmass) +
                    (5/Math.pow(R, 2.0)) * 
                    (
                        Math.pow(a, 2.0) 
                        + (Math.pow(b,2) * Math.pow(Math.cos(verangle),2)) 
                        + (Math.pow(c,2) * Math.pow(Math.sin(verangle),2))
                        - (2*b*c*Math.cos(verangle) * Math.sin(verangle))
                    )
                );
        v = new Vector(
                0,
                (-F/m) * Math.cos(verangle),
                (-F/m) * Math.sin(verangle)
                );
        double I = (0.4) * m * Math.pow(R,2);
        w = new Vector(
                (1/I) * ((-c * F * Math.sin(verangle)) + (b * F * Math.cos(verangle))),
                (1/I) * (a * F * Math.sin(verangle)),
                (1/I) * (-a * F * Math.cos(verangle))
                );
    }

    @Override
    public String toString() {
        String returnstring = "";
        if (p != null)
                returnstring = "Position: \n" +
                p.getX() + "," + p.getY() + "," + p.getZ() + "\n";
        if (v != null)
                returnstring += "Velocity: \n" +
                v.getX() + "," + v.getY() + "," + v.getZ() + "\n";
        if (w != null)
                returnstring += "Spin: \n" +
                w.getX() + "," + w.getY() + "," + w.getZ() + "\n";
        return returnstring;
    }


}
